﻿using System.Collections.Generic;
using System.ComponentModel;
using Assets.Scripts.Net;
using Assets.Scripts.Tool;
using DG.DemiLib;
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Scripts.UI.Panel
{
    public class SettingPanel : UIBase<SettingPanel>
    {
        public Toggle Toggle_1080P;
        public Toggle Toggle_900P;
        public Toggle Toggle_720P;
        public Transform OriginTipInfo; 
        public Transform NewAgainTipInfo;
        public InputField OriginPassword;
        public InputField NewPassword;
        public InputField NewAgainPassword;
        public Button SureButton;
        public Button ExitButton;

        public const string RESOLUTION_HEIGHT= "RESOLUTION_HEIGHT";

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
            Toggle_1080P.onValueChanged.AddListener(x => {
                if (x) {
                    Screen.SetResolution(1920, 1080, false);
                    PlayerPrefs.SetInt(RESOLUTION_HEIGHT, 1080);
                }                              
            });
            Toggle_900P.onValueChanged.AddListener(x => {
                if (x) { 
                    Screen.SetResolution(1600, 900, false);
                    PlayerPrefs.SetInt(RESOLUTION_HEIGHT, 900);
                }                
            });
            Toggle_720P.onValueChanged.AddListener(x => {
                if (x) Screen.SetResolution(1280, 720, false);
                PlayerPrefs.SetInt(RESOLUTION_HEIGHT, 720);
            });
            SureButton.onClick.AddListener(OnSureButtonClick);
            ExitButton.onClick.AddListener(Exit);
            OriginTipInfo.gameObject.SetActive(false);
            NewAgainTipInfo.gameObject.SetActive(false);
            InitToggle();
        }

        private void InitToggle()   
        {           
            switch (PlayerPrefs.GetInt(RESOLUTION_HEIGHT))
            {
                case 1080:
                    Toggle_1080P.isOn = true;
                    break;
                case 900:
                    Toggle_900P.isOn = true;
                    break;
                case 720:
                    Toggle_720P.isOn = true;
                    break;
                default:
                    Toggle_1080P.isOn = true;
                    break;
            }
        }

        protected override UIType UIType
        {
            get { return UIType.Panel; }
        }

        protected override void Update()
        {
            if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
            {
                Debug.Log("enter点击事件，，，，，");
                OnSureButtonClick();
            }
        }

        private void OnSureButtonClick()
        {
            bool canChange = true;//是否能够修改密码
            string password = PlayerPrefs.GetString("Password");
            if (OriginPassword.text != password)
            {
                OriginTipInfo.gameObject.SetActive(true);
                canChange = false;
            }
            else
            {
                OriginTipInfo.gameObject.SetActive(false);
            }
            if (string.IsNullOrEmpty(NewPassword.text) || NewPassword.text != NewAgainPassword.text)
            {
                canChange = false;
                NewAgainTipInfo.gameObject.SetActive(true);
            }
            else
            {
                NewAgainTipInfo.gameObject.SetActive(false);
            }
            if (canChange)
            {
                //向服务器消息
                Dictionary<string, object> dict = NetManager.Instance.GetDict();
                string originPwd = Md5Encript.GetMd5HashStr(OriginPassword.text);
                string newPwd = Md5Encript.GetMd5HashStr(NewPassword.text);
                dict.Add("originpassword",originPwd);
                dict.Add("newpassword",newPwd);
                NetManager.Instance.SendRequest(RequestType.ChangePassword,dict, () =>
                {
                    UIManager._Instance.ShowTipInfo("密码更改成功");   
                    Exit();
                });
            }            
        }
    }
}